import TreeCesiumData from "./layer_tree_data";
import LayerManager from "@/plugins/mev/layer/LayerManager";
import * as Cesium from "cesium";
class ImageHPYData extends TreeCesiumData {
    constructor(data, viewer) {
        super(data, viewer);
        this._data = data;
        this._viewer = viewer;
        //通过路径生成的影像对象
        this._dataObject = null;
        this._imageType = "";
        if(this._data.path !== undefined)
        {
            this._path = this._data.basePath + ":" + this._data.path;
        }
        else
        {
            for(let i = 0; i < this._data.datalist.length; ++i)
            {
                if(i == (this._data.datalist.length - 1))
                {
                    this._path += this._data.datalist[i].path;
                }
                else
                {
                    this._path += (this._data.datalist[i].path + ":");
                }
            }
            this._path = this._data.basePath + ":" + this._path;
        }
        this._imageryIndex = 0;
    }

    show() {
        if(this._dataObject === null)
        {
            let info = JSON.parse(JavaContext.getHPYInfo(this._path));
            this._rect = new Cesium.Rectangle(Cesium.Math.toRadians(info.minx), Cesium.Math.toRadians(info.miny), Cesium.Math.toRadians(info.maxx), Cesium.Math.toRadians(info.maxy));
            this._imageType = info.imageType;
            this._dataObject = new Cesium.ImageryLayer(
                new Cesium.UrlTemplateImageryProvider({
                    url: this._path + "?datatype=image_hpy&z={z}&x={x}&y={y}&imageType=" + this._imageType
                })
            );
            this._viewer.imageryLayers.add(this._dataObject, this._imageryIndex);
        }


    }

    close() {
        if(this._dataObject !== null)
        {
            this._viewer.imageryLayers.remove(this._dataObject);
            this._dataObject = null;
        }
    }

    flyTo(){
        if(this._rect !== null)
        {
            this._viewer.camera.flyTo({
                destination : this._rect
            });
        }
    }

    updateInternalIndex(internalIndex){
        if(this._data.visible)
        {
            this._imageryIndex = internalIndex.imageryIndex;
            ++internalIndex.imageryIndex;
        }
    }
}

export default ImageHPYData;
